// 分页
var pageNum = 1, pageSize = 10, totalCount, totalPages, showStart, showEnd;
function exportFile(action, status) {
	var param = {};
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
	if (startDate > endDate) {
		layer.alert("开始日期不能大于结束日期", {
			icon : -1
		});
		return;
	}
	var obj = param.param;
	var searchStr = $("#searchTxt").val();
	if (!obj) {
		obj = {};
	}
	obj.searchStr = searchStr;
	if (startDate && endDate) {
		obj.startDate = startDate;
		obj.endDate = endDate;
	}
	if (status) {
		obj.status = $("#status").val();
		if (obj.status == "") {
			obj.status = null;
		}
	}
	param.param = JSON.stringify(obj);
	ajaxPost(param, action, function(data) {
		location.href = data.exportFile;
	})
}
function searchList(param, action, fun) {
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
	if (startDate > endDate) {
		layer.alert("开始日期不能大于结束日期", {
			icon : -1
		});
		return;
	}
	var obj = param.param;
	var searchStr = $("#searchTxt").val();
	if (!obj) {
		obj = {};
	}
	obj.searchStr = searchStr;
	if (startDate && endDate) {
		obj.startDate = startDate;
		obj.endDate = endDate;
	}
	param.param = JSON.stringify(obj);
	$("#data").children().remove();
	$.ajax({
		url : '/admin/xmlModel/model.xml',
		type : 'GET',
		dataType : 'xml',
		timeout : 1000, // 设定超时
		cache : false, // 禁用缓存
		error : function(xml) {
			console.log("加载XML文档出错!");
		},
		success : function(xml) {
			var modelXmlObj = {};
			$(xml).find("model").each(function(i) { // 查找所有student节点并遍历
				var id = $(this).children("id").text();
				var content = $(this).children("content").text();
				console.log(id)
				$.template(id, content);
			});
			ajaxPost(param, action, function(data) {
				totalPages = data.totalPages ? null : 0;
				totalCount = data.totalCount ? null : 0;
				filePath = data.filePath;
				fun(data.list);
				$(".pagePlugin").remove();
				showStart = (pageNum - 1) * pageSize + 1;
				showEnd = pageNum * pageSize;
				if (showEnd > totalCount)
					showEnd = totalCount;
				$.tmpl("page_t", {}).insertAfter($("table"));
				changePage();
			});
		}
	});
}
function search() {
	init();
}
function page(number) {
	localStorage.admin_order_page = "1";
	localStorage.web_order_page = "1";
	pageNum = number;
	init();
}
function changePage() {
	$("ul.pagination").children().remove();
	$.tmpl("page_previous_t", {}).appendTo($("ul.pagination"));
	if (totalPages < 8) {
		for (var i = 1; i <= totalPages; i++) {
			$.tmpl("page_normal_t", {
				number : i
			}).appendTo($("ul.pagination"));
		}
	} else {
		console.log(pageNum);
		if (pageNum <= 4) {
			for (var i = 1; i <= 5; i++) {
				$.tmpl("page_normal_t", {
					number : i
				}).appendTo($("ul.pagination"));
			}
			$.tmpl("page_point_t", {}).appendTo($("ul.pagination"));
			$.tmpl("page_normal_t", {
				number : totalPages
			}).appendTo($("ul.pagination"));
		} else if (pageNum >= totalPages - 4) {
			$.tmpl("page_normal_t", {
				number : 1
			}).appendTo($("ul.pagination"));
			$.tmpl("page_point_t", {}).appendTo($("ul.pagination"));
			for (var i = totalPages - 4; i <= totalPages; i++) {
				$.tmpl("page_normal_t", {
					number : i
				}).appendTo($("ul.pagination"));
			}
		} else {
			$.tmpl("page_normal_t", {
				number : 1
			}).appendTo($("ul.pagination"));
			$.tmpl("page_point_t", {}).appendTo($("ul.pagination"));
			for (var i = pageNum - 1; i <= pageNum + 1; i++) {
				$.tmpl("page_normal_t", {
					number : i
				}).appendTo($("ul.pagination"));
			}
			$.tmpl("page_point_t", {}).appendTo($("ul.pagination"));
			$.tmpl("page_normal_t", {
				number : totalPages
			}).appendTo($("ul.pagination"));
		}
		$(".pageNum").removeClass("active");
		$(".page" + pageNum).addClass("active");
	}
	$.tmpl("page_next_t", {}).appendTo($("ul.pagination"));
}
function nextPage(num) {
	localStorage.admin_order_page = "1";
	localStorage.web_order_page = "1";
	if (num == 1) {
		if (pageNum == 1)
			return;
		pageNum = pageNum - 1;
	}

	if (num == 2) {
		if (pageNum == totalPages)
			return;
		pageNum = pageNum + 1;
	}
	init();
}
